using System;
using System.IO;
using System.Collections.Generic;
using Subsync.Core.Sync;
using Subsync.Core.Subversion;

namespace Subsync.GUI
{
	/// <summary>
	/// Author: Steven Goh Jian Wen 
	/// Except methods with Author: Hoang Duc
	/// </summary>
    public enum SyncAddError
    {
        Ok, //no error
        SyncNameExists,
        FolderDoesNotExist,
		WorkingCopyWriteError,
        WorkingCopyPathError,
        SyncDoesNotExist,
        NotRepo,
        EmptyString,
    }

    public class Tools
    {
		public static bool IsWritable (string path)
		{
			string test_path = Path.Combine (path, Subsync.Tools.RandomStr ());

			while (!File.Exists (test_path))
				test_path = Path.Combine (path, Subsync.Tools.RandomStr ());

			try
            {
				TextWriter tw = new StreamWriter (Path.Combine (path, "___testfile___.txt"));
				tw.Close ();
			}
            catch (Exception)
            {
				return false;
			}
			return true;
		}

		/// <summary>
		/// Author: Hoang Duc
		/// </summary>			
        public static bool IsInsideAnotherWorkingCopy(string path)
        {
            string parent = Path.GetDirectoryName(path);
            if (parent == null) 
                return false;
            else if (IsVersioned(parent))
                return true;
            else if (IsInsideAnotherWorkingCopy(parent))
                return true;
            else
                return false;
        }
		
        /// <summary>
        /// Check that path exists and it isn't versioned.
        /// </summary>
        /// <param name="path">
        /// A <see cref="System.String"/>
        /// </param>
        /// <returns>
        /// A <see cref="System.Boolean"/>
        /// </returns>
        public static bool IsVersioned(string path)
        {
            if (Directory.Exists(path))
            {
                //make sure .svn folder does not exist inside.
                string[] subfolders = Directory.GetDirectories(path);
                foreach (string f in subfolders)
                {
                    if (f.Substring(f.LastIndexOf(Path.DirectorySeparatorChar) + 1) == ".svn")
                        return true;
                }
                return false;
            }
            else
                return true;
        }

		/// <summary>
		/// Author: Hoang Duc
		/// </summary>	
        public static bool FolderExists(string path)
        {
            return Directory.Exists(path);
        }

        public static bool SyncExists(string name)
        {
            try
            {
                new Config().LoadSync(name);
            }
            catch (SyncNotFound)
            {
                return false;
            }
            return true;
        }

        public static bool IsRepo(string path)
        {
            if (Directory.Exists(path))
            {
                string[] sub_folders = Directory.GetDirectories(path);

                if ((sub_folders.Length == 4) && (((IList<string>)sub_folders).Contains(Path.Combine(path, "conf"))))
                    return true;
            }
                return false;
        }

		/// <summary>
		/// Author: Hoang Duc
		/// </summary>	
        public static bool IsRemoteRepo(string path)
        {
            try
            {
                new SVNInfo(path, "head", "head").Execute();
            }
            catch (Exception)
            {
                return false;
            }
            return true;
        }
    }
}
